package com.sskj.functions.activity;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.BorrowDetailsPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StrategyBean;
import com.sskj.lib.bean.TabEntity;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.zhouyou.view.seekbar.SignSeekBar;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/07/04
 */
@Route(path = RConfig.FUNCTIONS_DETAILS)
public class BorrowDetailsActivity extends BaseActivity<BorrowDetailsPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.tvNh)
    TextView tvNh;
    @BindView(R2.id.tvTime)
    TextView tvTime;
    @BindView(R2.id.tv_pingcang)
    TextView tvPingcang;
    @BindView(R2.id.tv_jingjie)
    TextView tvJingjie;
    @BindView(R2.id.iv_jindu)
    ImageView ivJIndu;
    @BindView(R2.id.seekbar)
    SignSeekBar seekbar;
    @BindView(R2.id.tvMoney)
    TextView tvMoney;
    @BindView(R2.id.tvTime1)
    TextView tvTime1;
    @BindView(R2.id.tvTime2)
    TextView tvTime2;
    @BindView(R2.id.tvTime3)
    TextView tvTime3;
    @BindView(R2.id.tvTime4)
    TextView tvTime4;
    @BindView(R2.id.tvTime5)
    TextView tvTime5;
    @BindView(R2.id.btTouzi)
    Button btTouzi;
    @BindView(R2.id.tvProgress)
    TextView tvProgress;
    @BindView(R2.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R2.id.tv_msg)
    TextView tvMsg;
    @BindView(R2.id.tv_qixian)
    TextView tvQixian;
    @BindView(R2.id.tv_qitou)
    TextView tvQitou;
    @BindView(R2.id.tv_diya)
    TextView tvDiya;
    @BindView(R2.id.cs_diya)
    ConstraintLayout csDiya;
    @BindView(R2.id.cs_lines)
    ConstraintLayout csLines;
    @BindView(R2.id.ll_jindu)
    LinearLayout llJIndu;
    private String pay_type = "usdt";
    private int page = 1;
    @Autowired
    public int itemType;
    @Autowired
    public String type;
    @Autowired
    public StrategyBean.List1Bean List1Bean;
    @Autowired
    public StrategyBean.List2Bean List2Bean;
    private StrategyBean data;
    private String[] titles = new String[]{App.INSTANCE.getString(R.string.functions_borrowDetailsActivity1), App.INSTANCE.getString(R.string.functions_borrowDetailsActivity2)};
    private String msg_id;
    private double progress;
    private String languages = "1";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_borrow_details;
    }

    @Override
    public BorrowDetailsPresenter getPresenter() {
        return new BorrowDetailsPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);

        if (type.equals(Constans.FUNCTIONS_CLTZ)) {
            csDiya.setVisibility(View.GONE);
            tvTime.setVisibility(View.VISIBLE);
            csLines.setVisibility(View.GONE);
            llJIndu.setVisibility(View.VISIBLE);
        }
        if (type.equals(Constans.FUNCTIONS_JBSX)) {
            tvTime.setVisibility(View.GONE);
            csDiya.setVisibility(View.VISIBLE);
            csLines.setVisibility(View.VISIBLE);
            llJIndu.setVisibility(View.GONE);
        }

        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            mTabEntities.add(new TabEntity(titles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (itemType) {
                    case 0://可投资
                        switch (position) {
                            case 0:
                                RichText.from(List1Bean.getContent()).into(tvMsg);
                                break;
                            case 1:
                                RichText.from(List1Bean.getRisk_management()).into(tvMsg);
                                break;
                            default:
                                break;
                        }
                        break;
                    case 1://不可投资
                        switch (position) {
                            case 0:
                                RichText.from(List2Bean.getContent()).into(tvMsg);
                                break;
                            case 1:
                                RichText.from(List2Bean.getRisk_management()).into(tvMsg);
                                break;
                            default:
                                break;
                        }
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        switch (itemType) {
            case 0://可投资
                updateUnfinishedUI(List1Bean);
                if (type.equals(Constans.FUNCTIONS_JBSX)) {
                    initJindu(List1Bean.getWarehouse_closing_line(), List1Bean.getRisk_line());
                }
                break;
            case 1://不可投资
                updateFinishedUI(List2Bean);
                if (type.equals(Constans.FUNCTIONS_JBSX)) {
                    initJindu(List2Bean.getWarehouse_closing_line(), List2Bean.getRisk_line());
                }
                btTouzi.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    private void initJindu(String pingCangLine, String jingjieLine) {
        ivJIndu.post(() -> {
            ConstraintLayout.LayoutParams jInduLayoutParams = (ConstraintLayout.LayoutParams) ivJIndu.getLayoutParams();
            jInduLayoutParams.leftMargin = tvJingjie.getWidth() / 2;
            jInduLayoutParams.rightMargin = tvJingjie.getWidth() / 2;
            ivJIndu.setLayoutParams(jInduLayoutParams);
            if (!"0".equals(pingCangLine)) {
                ConstraintLayout.LayoutParams jingjieLayoutParams = (ConstraintLayout.LayoutParams) tvJingjie.getLayoutParams();
                if (Integer.valueOf(pingCangLine) > 50) {
                    jingjieLayoutParams.leftMargin = csLines.getWidth() / 100 * Integer.valueOf(pingCangLine) - tvJingjie.getWidth();
                } else {
                    jingjieLayoutParams.leftMargin = csLines.getWidth() / 100 * Integer.valueOf(pingCangLine) - tvJingjie.getWidth() / 2;
                }
                tvJingjie.setLayoutParams(jingjieLayoutParams);
                tvJingjie.setText(App.INSTANCE.getString(R.string.pingcangxian) + "(" + pingCangLine + "%)");
            }
            if (!"0".equals(jingjieLine)) {
                ConstraintLayout.LayoutParams jingjieLayoutParams = (ConstraintLayout.LayoutParams) tvPingcang.getLayoutParams();
                if (Integer.valueOf(jingjieLine) > 50) {
                    jingjieLayoutParams.leftMargin = csLines.getWidth() / 100 * Integer.valueOf(jingjieLine) - tvPingcang.getWidth();
                } else {
                    jingjieLayoutParams.leftMargin = csLines.getWidth() / 100 * Integer.valueOf(jingjieLine) - tvPingcang.getWidth() / 2;
                }
                tvPingcang.setLayoutParams(jingjieLayoutParams);
                tvPingcang.setText(App.INSTANCE.getString(R.string.jingjiexian) + "(" + jingjieLine + "%)");
            }
        });
    }

    private void updateUnfinishedUI(StrategyBean.List1Bean list1Bean) {
        if (type.equals(Constans.FUNCTIONS_JBSX)) {
            tvQixian.setText(list1Bean.getCycle() + App.INSTANCE.getString(R.string.tiam));
            tvQitou.setText(CommonUtil.dealCoinNum(list1Bean.getShare_min(), list1Bean.getPay_type().toUpperCase()));
            tvDiya.setText(list1Bean.getMortgage_assets());
        }


        tvTitle.setText(list1Bean.getName());
        tvNh.setText(NumberUtil.keep2(list1Bean.getInterest_rate()) + "%");
        tvTime.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity3) + list1Bean.getCycle() + App.INSTANCE.getString(R.string.functions_borrowDetailsActivity4) + CommonUtil.dealCoinNum(list1Bean.getShare_min(), list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
        tvMoney.setText(String.format(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity5), CommonUtil.dealCoinNum(list1Bean.getSurplus(), list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase()));
        tvTime1.setText(TimeFormatUtil.formatE(Long.valueOf(list1Bean.getInterest_raising_time_s()) * 1000));
        tvTime2.setText(TimeFormatUtil.formatE(Long.valueOf(list1Bean.getMaturity_time_s()) * 1000));
        tvTime3.setText(TimeFormatUtil.formatB(Long.valueOf(list1Bean.getInterest_raising_time()) * 1000) + App.INSTANCE.getString(R.string.functions_borrowDetailsActivity6) + TimeFormatUtil.formatB(Long.valueOf(list1Bean.getMaturity_time()) * 1000));
        if (list1Bean.getPayment_method().equals("1")) {
            tvTime4.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity7));
        } else {
            tvTime4.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity8));
        }
        double div = 0;
        try {
            div = ArithUtil.div(Double.parseDouble(list1Bean.getSurplus()), Double.parseDouble(list1Bean.getTotal_price()), 2);
            Log.e("div", div + "");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        progress = ArithUtil.sub(100, ArithUtil.mul(div, 100));
        seekbar.setEnabled(false);
        seekbar.getConfigBuilder().floatType().min(0).max(100).build();
        //进图条进度
        seekbar.setProgress((int) progress);
        tvProgress.setText(progress + "%");
        tvTime5.setText(CommonUtil.dealCoinNum(list1Bean.getTotal_price(), list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
        msg_id = list1Bean.getId();
        RichText.from(list1Bean.getContent()).into(tvMsg);
    }

    private void updateFinishedUI(StrategyBean.List2Bean list2Bean) {
        if (type.equals(Constans.FUNCTIONS_JBSX)) {
            tvQixian.setText(list2Bean.getCycle() + App.INSTANCE.getString(R.string.tiam));
            tvQitou.setText(CommonUtil.dealCoinNum(list2Bean.getShare_min(), list2Bean.getPay_type().toUpperCase()));
            tvDiya.setText(list2Bean.getMortgage_assets());
        }
        tvTitle.setText(list2Bean.getName());
        tvNh.setText(list2Bean.getInterest_rate() + "%");
        tvTime.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity3) + list2Bean.getCycle() + App.INSTANCE.getString(R.string.functions_borrowDetailsActivity4) + CommonUtil.dealCoinNum(list2Bean.getShare_min(), list2Bean.getPay_type().toUpperCase()) + list2Bean.getPay_type().toUpperCase());
        tvMoney.setText(String.format(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity5), CommonUtil.dealCoinNum(list2Bean.getSurplus(), list2Bean.getPay_type().toUpperCase()) + list2Bean.getPay_type().toUpperCase()));
        tvTime1.setText(TimeFormatUtil.formatE(Long.valueOf(list2Bean.getInterest_raising_time_s()) * 1000));
        tvTime2.setText(TimeFormatUtil.formatE(Long.valueOf(list2Bean.getMaturity_time_s()) * 1000));
        tvTime3.setText(TimeFormatUtil.formatB(Long.valueOf(list2Bean.getInterest_raising_time()) * 1000) + App.INSTANCE.getString(R.string.functions_borrowDetailsActivity6) + TimeFormatUtil.formatB(Long.valueOf(list2Bean.getMaturity_time()) * 1000));
        if (list2Bean.getPayment_method().equals("1")) {
            tvTime4.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity7));
        } else {
            tvTime4.setText(App.INSTANCE.getString(R.string.functions_borrowDetailsActivity8));
        }
        double div = 0;
        try {
            div = ArithUtil.div(Double.parseDouble(list2Bean.getSurplus()), Double.parseDouble(list2Bean.getTotal_price()), 2);
            Log.e("div", div + "");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        progress = ArithUtil.sub(100, ArithUtil.mul(div, 100));

        seekbar.setEnabled(false);
        seekbar.getConfigBuilder().floatType().min(0).max(100).build();
        //进图条进度
        seekbar.setProgress((int) progress);
        tvProgress.setText(progress + "%");
        tvTime5.setText(CommonUtil.dealCoinNum(list2Bean.getTotal_price(), list2Bean.getPay_type().toUpperCase()) + list2Bean.getPay_type().toUpperCase());
        msg_id = list2Bean.getId();
        RichText.from(list2Bean.getContent()).into(tvMsg);
    }

    @Override
    public void initData() {
        ClickUtil.click(300, btTouzi, () -> {
            if (type.equals(Constans.FUNCTIONS_CLTZ)) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_INVEST)
                        .withSerializable("List1Bean", List1Bean)
                        .navigation(this);
            }
            if (type.equals(Constans.FUNCTIONS_JBSX)) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_STRATEGYDETAILS)
                        .withSerializable("List1Bean", List1Bean)
                        .navigation(this);
            }
        });
    }
}
